package com.cherry.openlib.datebase.greendao

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.cherry.openlib.R
import com.cherry.openlib.datebase.greendao.db.AuthorDao
import com.cherry.openlib.datebase.greendao.db.DaoMaster
import com.cherry.openlib.datebase.greendao.db.DaoSession
import kotlinx.android.synthetic.main.activity_lite_pal_test.*
import java.lang.StringBuilder
import java.util.*
import kotlin.properties.Delegates

/**
 * https://juejin.im/post/5959b5bcf265da6c4d1bb245#heading-9
 *DaoMaster
 * DaoSession
 *XxxDAO
 *Entities
 */
class GreenDAOTestActivity : AppCompatActivity() {

    private var daoSession: DaoSession by Delegates.notNull()

    private var authorDao: AuthorDao by Delegates.notNull()

    private val totalList = arrayListOf<Author>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_green_dao_test)
        initDB()
    }

    /**
     * 一般在Application中初始化
     */
    private fun initDB() {
        val helper = DaoMaster.DevOpenHelper(applicationContext, "test_greenDAO.db")
        val database = helper.writableDatabase
        val daoMaster = DaoMaster(database)
        daoSession = daoMaster.newSession()
        authorDao = daoSession.authorDao
    }

    fun queryAll(view: View) {
        val list = authorDao.loadAll()
        totalList.clear()
        totalList.addAll(list)
        if (list.isNotEmpty()) {
            val builder = StringBuilder()
            list.forEach {
                builder.append(it.id).append("\t").append(it.name).append("\t").append(it.belongCompany)
                        .append("\t").append(Date(it.birthday).toLocaleString()).append("\t").append("\n")
            }
            result.text = builder.toString()
        } else {
            result.text = ""
        }
    }


    fun addFive(view: View) {
//        authorDao.insertOrReplace(Author(Date().time, "韩寒", "艾润", Date().time))
        val listAuthor = listOf(Author(System.currentTimeMillis() + Random().nextInt(100), "韩寒1号", "艾润", Date().time),
                Author(System.currentTimeMillis() + Random().nextInt(100), "韩寒2号", "艾润", Date().time),
                Author(System.currentTimeMillis() + Random().nextInt(100), "韩寒3号", "艾润", Date().time),
                Author(System.currentTimeMillis() + Random().nextInt(100), "韩寒4号", "艾润", Date().time),
                Author(System.currentTimeMillis() + Random().nextInt(100), "韩寒5号", "艾润", Date().time))
        authorDao.insertOrReplaceInTx(listAuthor)
    }

    fun delete(view: View) {
        if (totalList.isNotEmpty()) {
            authorDao.delete(totalList[0])
            totalList.removeAt(0)
        }
        //todo 根据条件删除 ???
        if (input.text.toString().isNotEmpty()) {

        }
    }

    fun queryWhere(view: View) {
        if (input.text.toString().isNotEmpty()) {
            //语句
//            val list = authorDao.queryRaw("where name like %${input.text}%", input.text.toString())
            val list = authorDao.queryBuilder()
                    .where(AuthorDao.Properties.Name.like("%${input.text}%"))
                    .build()
                    .list()
            if (list.isNotEmpty()) {
                val builder = StringBuilder()
                list.forEach {
                    builder.append(it.id).append("\t").append(it.name).append("\t").append(it.belongCompany)
                            .append("\t").append(Date(it.birthday).toLocaleString()).append("\t").append("\n")
                }
                result.text = builder.toString()
            } else {
                result.text = ""
            }
        }
    }

    fun update(view: View) {
        if (totalList.isNotEmpty()) {
            val author = totalList[0]
            author.name = "三重门"
            authorDao.update(author)
        }

        //todo 根据条件更新 ???
        if (input.text.toString().isNotEmpty()) {

        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}
